//
//  MainTabBarController+JJMesssageEvent.m
//  YYFMMall
//
//  Created by chenzm on 2019/11/6.
//  Copyright © 2019 YuYou1. All rights reserved.
//

#import "MainTabBarController+JJMesssageEvent.h"

#import "AppHomeVC.h"
#import "SecondHomeVC.h"
#import "ThirdHomeVC.h"
#import "MyHomeVC.h"
#import "PersonalVC.h"
#import "ZMWkwebViewVC.h"

@implementation MainTabBarController (JJMesssageEvent)

- (void)jj_messagePushVC:(UIViewController *)superVC url:(NSString *)urlStr
                   title:(NSString *)title sourceType:(SourceType)sourceType {
    /*urlStr链接规则：
     app内部跳转：url.scheme://url.query?键=值&键=值【fmuser://circum_shop_detail?id=10004】
     跳转h5页面：https://www.baidu.com/
     */
    NSURL *url = kURL([urlStr stringByReplacingOccurrencesOfString:@" " withString:@""]);
    UIViewController *topVC = nil;
    if ([url.scheme isEqualToString:@"fmuser"]) {
        //原生页面
        NSString *hostStr = kIsEmptyStr(urlStr)?@"":urlStr;
        NSDictionary *para = [url.query jj_urlStringParamDic];
#pragma mark- 首页
        if ([hostStr containsString:@"home/index"]){
            if (superVC) {
                [superVC.navigationController popToRootViewControllerAnimated:YES];
            }else{
                [kAppDelegate initRootWithType:1 vc:nil];
            }
            kAppDelegate.tabBarCt.selectedIndex = 0;
            return;
        }
#pragma mark- 我的
        else if ([hostStr containsString:@"my/personalInfo"]){
            // 限时活动
            PersonalVC *vc = [PersonalVC new];
            topVC = vc;
        }
#pragma mark- 其他
        if (topVC) {
            [self jj_pushVC:topVC superVC:superVC sourceType:sourceType];
        }
    } else {
            //web页面
            if (urlStr.length) {
                /// 添加标题
                NSURL *url = kURL([urlStr stringByReplacingOccurrencesOfString:@" " withString:@""]);
                NSDictionary *para = [url.query jj_urlStringParamDic];
                NSString *navTitle = @"";
                if ([title isEqualToString:@"1"]) {
                    navTitle = [para jj_stringForKey:@"title"];
                } else {
                    navTitle = title;
                }
                /// 添加userId
                NSMutableString *string = [NSMutableString stringWithString:urlStr];
                if ([string containsString:@"id="]) {
                    NSRange range = [string rangeOfString:@"id="];
                    [string insertString:[UserInfoM getUserInfo].userId atIndex:range.location+range.length];
                }
                if (![urlStr containsString:@"http"]) {
                    return;
                }
                
                if (!superVC) {
                    [superVC jj_webView:kIsEmptyStr(navTitle)?@"":navTitle url:urlStr viewTransitions:sourceType];
                }else{
                    RTRootNavigationController *nav =  self.selectedViewController;
                    [nav jj_webView:kIsEmptyStr(navTitle)?@"":navTitle url:urlStr viewTransitions:sourceType];
                }
            }
        }
}

/// 控制器跳转
- (void)jj_pushVC:(UIViewController *)vc superVC:(UIViewController *)superVC sourceType:(SourceType)souceType {
    if (souceType == SourceTypeSplash) {
        RTRootNavigationController *nav= [[RTRootNavigationController alloc]initWithRootViewController:vc];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:YES completion:nil];
    } else {
        vc.hidesBottomBarWhenPushed = YES;
        if (superVC) {
            [superVC.navigationController pushViewController:vc animated:YES];
        }else{
            RTRootNavigationController *nav =  self.selectedViewController;
            [nav pushViewController:vc animated:YES];
        }
    }
}

@end
